package com.study.configuration;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.study.dto.Tang;
import java.util.function.Supplier;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

/**
 * @Author tanglingyu
 * @Date: 2021/04/21/ 12:08
 * @Description
 */
@Configuration
public class MyConfiguration {

  /**
   * spring方面
   * step1:beanFactory DefaultListableBeanFactory(设置参数BeanPostProcessor,Aware子类)
   * step2:bean解析BeanDefinition
   * step3:BeanFactoryPostProcessor(PlaceholderConfigurerSupport,ConfigurationClassPostProcessor)占位符和@Configuration的解析
   * step4:beanPostProcessor拓展点
   * step5:反射new beanDefinition实例
   * step6:beanPostProcessor执行前置处理 init-method 执行后置处理 @PostConstruct
   * step7:singletonObjects getObject()
   * step8:销毁
   * @return
   */

  @Bean
  public Tang tang() {
    Supplier<Tang> supplier = Tang::new;
    Tang tang = supplier.get();
    tang.setName("汤凌宇123");
    return tang;
  }

  @Bean
  public HttpMessageConverters fastJsonHttpMessageConverters() {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter<?> converter = fastConverter;
    return new HttpMessageConverters(converter);
  }
}
